<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\LevCourse;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class LevCourseController extends AdminController
{


    public function index(Content $content)
    {
        return $content->title('休课')
            ->description('列表')
            ->body($this->grid());
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new LevCourse(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id','姓名')->display(function ($value){
                $res = \App\Models\User::where('id',$value)->value('user_name');
                return $res ?? '';
            });
            $grid->img('休课证明')->display(function ($pictures) {
                return json_decode($pictures, true);
            })->image(config('alioss.ossServer'),80,80);
            $grid->column('causation');
            $grid->column('created_at')->display(function ($value){
                return $res = date('Y-m-d H:i:s',$value) ?? '';
            });
            $grid->column('status','审核操作')->display(function ($value){
                if ($value == 0){
                    return "
                    <button class='btn btn-sm btn-custom'  id='data-" . $this->id . "'>通过</button>
                           <script>
                                   $('#data-" . $this->id . "').unbind('click').click(function() {
                      Dcat.confirm('确认休课', null, function () {
                          $.post({
                                    method: 'get',
                                    url: '/admin/LevCourse/" . $this->id . "/change',
                                    success: function (data) {
                                        if (typeof data === 'object') {
                                            if (data.status) {
                                                Dcat.swal.success('休课成功');
                                                window.location.reload();
                                            } else {
                                                Dcat.swal.error('休课失败');
                                            }
                                        }
                                    }
                                });
                              });
                             })
                           </script>
                    ";
                }else{
                    return "
                    <button class='btn btn-sm btn-danger' id='data-" . $this->id . "'>撤销</button>
                           <script>
                                   $('#data-" . $this->id . "').unbind('click').click(function() {
                      Dcat.confirm('取消激活', null, function () {
                          $.post({
                                    method: 'get',
                                    url: '/admin/LevCourse/" . $this->id . "/change1',
                                    success: function (data) {
                                        if (typeof data === 'object') {
                                            if (data.status) {
                                                Dcat.swal.success('取消成功');
                                                window.location.reload();
                                            } else {
                                                Dcat.swal.error('取消失败');
                                            }
                                        }
                                    }
                                });
                              });
                             })
                           </script>


                    ";
                }

            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
//                $actions->disableEdit();
//                $actions->disableQuickEidt();
                $actions->disableView();
            });
            $grid->disableCreateButton();

        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new LevCourse(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->multipleFile('img')->saving(function ($paths) {
                // 也可以转化为json
                return json_encode($paths);
            });
            $form->text('causation');
            $form->text('created_at');
        });
    }


    //审核操作
    public function change(Request $request){
        $id = $request['id'];
        DB::beginTransaction();
        $user_id = DB::table('lev_course')->where('id',$id)->value('user_id');
        $res = DB::table('lev_course')->where('id',$id)->update(['status'=>1]);
        $res1 = DB::table('user')->where('id',$user_id)->update(['leastatus'=>1]);
        if ($res && $res1){
            DB::commit();
            return response()->json([
                'status'  => true,
                'message' => '休课成功',
            ]);
        }else{
            DB::rollBack();
            return response()->json([
                'status'  => true,
                'message' => '休课失败',
            ]);
        }
    }
    //取消操作
    public function change1(Request $request){
        $id = $request['id'];
        $user_id = DB::table('lev_course')->where('id',$id)->value('user_id');
        $res = DB::table('lev_course')->where('id',$id)->update(['status'=>0]);
        $res1 = DB::table('user')->where('id',$user_id)->update(['leastatus'=>0]);
        if ($res && $res1){
            return response()->json([
                'status'  => true,
                'message' => '取消成功',
            ]);
        }else{
            return response()->json([
                'status'  => true,
                'message' => '取消失败',
            ]);
        }
    }
}
